-------------------------------------------------------------------
--                        AmB Themes                             --
-------------------------------------------------------------------

-- STOP.. YOU DONT NEED TO BE IN THIS FILE. Go to themelist.lua to register
-- your theme.

-- List of loaded themes.
AmB.Theme = { }

if SERVER then
	AddCSLuaFile("theme/themelist.lua")
	include("theme/themelist.lua")
else


	AmB.CurrentTheme = { Current = "" }
	setmetatable(AmB.CurrentTheme, {
		__index = function(t, k)
			local currentname = rawget(t, "Current")
			local theme = AmB.Theme[currentname]
			if theme != nil then
				return theme[k]
			else
				return nil
			end
		end
	})

	-- Sets the current theme by the theme name. Returns true
	-- if the new theme is loaded.
	function AmB.SetTheme(Name)
		t = AmB.Theme[Name]
		if t != nil then
			rawset(AmB.CurrentTheme, "Current", Name)
			return true
		end
		return false
	end
	
	-- Gets the value of a variable in teh theme. This is a ref
	-- so freeze may be applied to it.
	function ThemeVar(VarName)
		return Ref(AmB.CurrentTheme, VarName)
	end
	AmB.GetThemeVar = ThemeVar
	
	include("theme/themelist.lua")
end

-- Insure the existance of some commands needed to define themes
if Color == nil then
	function Color(r, g, b, a)
		return {r = r, g = g, b = b, a = a}
	end
end

-- Get themes from theme list.
local firsttheme = AmB.Theme[1]
local mtheme = { }
for i, v in ipairs(AmB.Theme) do
	local name = v
	local file = "theme/themes/" .. name .. ".lua"
	if SERVER then
		AddCSLuaFile(file)
	end
	
	-- Table to set theme vars
	THEME = { }
	
	-- Function to inherit other themes
	THEME_INHERIT = function(theme)
		mt = { }
		mt.__index = function(t, k)
			return AmB.Theme[theme][k]
		end
		setmetatable(THEME, mt)
	end
	
	-- Create the theme
	include(file)
	mtheme[name] = THEME
	
	-- Clear
	THEME_INHERIT = nil
	THEME = nil
end
AmB.Theme = mtheme
if CLIENT then
	AmB.SetTheme(firsttheme)
	
	--AWESOME COMMAND FOR TESTING/CREATING THEMES
	concommand.Add("amb_set_themevar", function(Player, Command, Args)
			VAL = nil
			local cstring = ""
			for x = 2, #Args do
				cstring = cstring .. " " .. Args[x]
			end
			pcall(function() RunString("VAL = " .. cstring) end)
			AmB.Theme[rawget(AmB.CurrentTheme, "Current")][Args[1]] = VAL
		end, function(Command, Args)
			tl = { }
			for k, v in pairs(AmB.Theme[rawget(AmB.CurrentTheme, "Current")]) do
				table.insert(tl, Command .. " \"" .. k .. "\"")
			end
			return tl
		end)
	concommand.Add("amb_get_themevar", function(Player, Command, Args)
			val = AmB.Theme[rawget(AmB.CurrentTheme, "Current")][Args[1]]
			if type(val) == "table" then
				PrintTable(val)
			else
				print(val)
			end
		end, function(Command, Args)
			tl = { }
			for k, v in pairs(AmB.Theme[rawget(AmB.CurrentTheme, "Current")]) do
				table.insert(tl, Command .. " \"" .. k .. "\"")
			end
			return tl
		end)
	concommand.Add("amb_set_theme", function(Player, Command, Args)
			AmB.SetTheme(Args[1])
		end, function(Command, Args)
			tl = { }
			for k, v in pairs(AmB.Theme) do
				table.insert(tl, Command .. " \"" .. k .. "\"")
			end
			return tl
		end)
end